//
//  KDChindWorkingReportController.m
//  WangJingApp
//
//  Created by 魏强 on 17/11/2.
//  Copyright © 2017年 KDTech. All rights reserved.
//

#import "KDChildWorkingReportController.h"
#import "KDTabBarController.h"
#import "KDWorkingReportTool.h"
#import "KDGetWorkingReportListParam.h"
#import "KDSearchView.h"
#import "KDWorkingReportCell.h"
#import "KDDetailReportController.h"
#import "SVProgressHUD.h"
#import "UITableView+FDTemplateLayoutCell.h"

@interface KDChildWorkingReportController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,KDSearchViewDelegate,KDWorkingReportCellDelegate>
@property (nonatomic, weak) UITableView *currentTabV;
@property (nonatomic, weak) KDSearchView *searchView;
/**  所有数据汇总 */
@property (nonatomic, strong) NSArray *infoArr;
/**
 *  加载第几页
 */
//@property (nonatomic, copy) NSString *page;
/**  <#Description#> */
@property (nonatomic, strong) KDGetWorkingReportListParam *param;

@end
static CGFloat const searchViewH = 35;
static CGFloat const searchViewVMagin = 7;
@implementation KDChildWorkingReportController
#pragma mark - 懒加载区
- (KDGetWorkingReportListParam *)param
{
    if (!_param) {
        _param = [[KDGetWorkingReportListParam alloc]init];
        _param.method = @"report/listSend";
        _param.logsType = [NSString stringWithFormat:@"%ld",self.type];
        _param.sendOrReceive = @"1";
//        _param.page = @"1";
        _param.limit = @"10";
        //        _param.
        //        _param
    }
    return _param;
}
- (NSArray *)infoArr
{
    if (!_infoArr) {
        _infoArr = [NSArray new];
    }
    return _infoArr;
}

- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    if (selected) {
        [self.currentTabV.header beginRefreshing];
    }
}

#pragma mark - 生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSearchView];
    [self setUpTableView];
    
}
#pragma mark - 板块布置区
/**
 *  初始化searchView
 */
- (void)setupSearchView{
    KDSearchView *searchView = [[KDSearchView alloc]init];
    searchView.frame = CGRectMake(10, searchViewVMagin, screenW - 20, searchViewH);
    searchView.placeholder = @"请输入关键词";
    searchView.returnSearch = YES;
    searchView.SearchViewBackGroundColor = WQColor(255, 255, 255);
    searchView.radius = 0.02;
    searchView.delegate = self;
    searchView.textColor = [UIColor darkGrayColor];
    searchView.layer.cornerRadius = 8;
    searchView.layer.borderWidth = 1;
    searchView.layer.borderColor = WQColor(238, 241, 243).CGColor;
    self.searchView = searchView;
    [self.view addSubview:searchView];
    
}
- (void)setUpTableView
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0  , searchViewH +2*searchViewVMagin, screenW, self.view.height -88 - searchViewH -2*searchViewVMagin) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tag = 1000;
    tableView.tableFooterView = [UIView new];
//    tableView.separatorColor = BackGroundColor;
    tableView.backgroundColor = BackGroundColor;
    tableView.rowHeight = Cell_h;
//    if (!iOS7) {
//        tableView.estimatedRowHeight = Cell_h;
//    }
    self.currentTabV = tableView;
    [self.view addSubview:tableView];
    UINib *xib =[UINib nibWithNibName:@"KDWorkingReportCell" bundle:nil] ;
    [tableView registerNib:xib forCellReuseIdentifier:@"KDWorkingReportCell"];
    tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadDetailData)];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDetailData)];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    // 设置footer
    self.currentTabV.footer = footer;
    [self.currentTabV.header beginRefreshing];
}
#pragma mark - 事件点击区

#pragma mark - 网络请求区
- (void)loadDetailData{
    self.param.page = @"1";

    [KDWorkingReportTool getWorkingReportListWithParam:self.param success:^(KDBaseResult *responseObejct) {
        if ([responseObejct.code isEqualToString:@"1"]) {
            NSArray *arr = responseObejct.data[@"data"];
            self.infoArr = [KDWorkingReportModel objectArrayWithKeyValuesArray:arr];
            [self.currentTabV.header endRefreshing];
            [self.currentTabV reloadData];
            
        }else if ([responseObejct.code isEqualToString:@"E040110000001"]){
            [self.navigationController dismissViewControllerAnimated:NO completion:^{
                KDNewfeatureController *new = [[KDNewfeatureController alloc]init];
                __block KDTabBarController*ctr = (KDTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                [UIApplication sharedApplication].keyWindow.rootViewController = new;
                [ctr removeObserver];
                [ctr removeFromParentViewController];
                ctr = nil;
            }];
        }else{
            [SVProgressHUD showErrorWithStatus:responseObejct.msg maskType:SVProgressHUDMaskTypeGradient];
        }
        [self.currentTabV.header endRefreshing];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误！" maskType:SVProgressHUDMaskTypeGradient];
        [self.currentTabV.header endRefreshing];
        KDLog(@"%@",error);
    }];
    
}
- (void)loadMoreDetailData{
    self.param.page = [NSString stringWithFormat:@"%ld",self.param.page.integerValue+1];
    
    [KDWorkingReportTool getWorkingReportListWithParam:self.param success:^(KDBaseResult *responseObejct) {
        if ([responseObejct.code isEqualToString:@"1"]) {
            NSArray *arr1 = [KDWorkingReportModel objectArrayWithKeyValuesArray:responseObejct.data[@"data"]];
            if (arr1.count) {
                NSMutableArray *arr = [NSMutableArray arrayWithArray:self.infoArr];
                [arr addObjectsFromArray:arr1] ;
                self.infoArr = arr;
                [self.currentTabV reloadData];
                [self.currentTabV.footer endRefreshing];
            }else{
                [self.currentTabV reloadData];
                [self.currentTabV.footer noticeNoMoreData];
            }
            
        }else{
            [SVProgressHUD showErrorWithStatus:responseObejct.msg maskType:SVProgressHUDMaskTypeGradient];
        }
    } failure:^(NSError *error) {
        [self.currentTabV.footer endRefreshing];
        [SVProgressHUD showErrorWithStatus:@"网络错误！" maskType:SVProgressHUDMaskTypeGradient];
        KDLog(@"%@",error);
    }];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infoArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KDWorkingReportCell *cell = [KDWorkingReportCell cellWithTableView:tableView];
    //    KDLog(@"waimian%d", indexPath.row);
    cell.delegate = self;
    if (tableView == self.currentTabV)
    {
        cell.delegate = self;
        if (self.infoArr.count != 0)
        {
            cell.model = self.infoArr[indexPath.row];
        }
        return cell;
    }else{
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:@"KDWorkingReportCell" configuration:^(KDWorkingReportCell * cell) {
        cell.model = self.infoArr[indexPath.row];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KDDetailReportController *detail = [[KDDetailReportController alloc]init];
    detail.model = self.infoArr[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
}
#pragma mark - 通知接收区


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - KDSearchViewDelegate
- (void)KDSearchViewWithSearchBtnClick:(KDSearchView *)searchView{
    self.param.keywords = searchView.text;
    [self.currentTabV.header beginRefreshing];
    self.param.keywords = nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
